package com.learn.core.controller.view;


import com.learn.common.utils.video.FileUploadTool;
import com.learn.core.domain.LMedia;
import com.learn.core.service.ILMediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


@RestController
public class VideoController {


    @Autowired
    private ILMediaService ilMediaService;

    @RequestMapping(value = "/upload")
    @ResponseBody
    public ModelAndView upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile,
                               HttpServletRequest request, ModelMap map) {
        String message = "";
        LMedia entity = new LMedia();
        String logoPathDir=request.getParameter("shipin");
        System.out.println("-------"+logoPathDir+"----------------------------------");
        FileUploadTool fileUploadTool = new FileUploadTool();
        try {
            entity = fileUploadTool.createFile(logoPathDir,multipartFile, request);
            if (entity != null) {
                ilMediaService.save(entity);
                message = "上传成功";
                map.put("entity", entity);
                map.put("result", message);
            } else {
                message = "上传失败";
                map.put("result", message);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ModelAndView("/pages/views/result", map);
    }

    @RequestMapping(value = "/{id}/play")
    @ResponseBody
    public ModelAndView playVideo(@PathVariable("id") long id, ModelMap model) {


// CharterDto charterDto_ = charterFacade.getCharterById(id);
        LMedia entity = ilMediaService.getById(id);
        model.put("entity", entity.getPath());
        ModelAndView view = new ModelAndView("index", model);
        return view;
    }

    @RequestMapping("/show")
    @ResponseBody
    public ModelAndView showList(HttpServletRequest request, ModelMap map) {
// 获取上传文件目录
        String logoPathDir = "/video/";
        String uploadFilePath =request.getSession().getServletContext().getRealPath(logoPathDir);
// 存储要下载的文件名
        List<String> fileNameMap = new ArrayList<String>();
        this.listFile(new File(uploadFilePath), fileNameMap);
        map.put("list", fileNameMap);
        return new ModelAndView("listFile", map);


    }


    /**
     * @Description: 递归遍历指定目录下的所有文件
     * @param file：即代表一个文件，也代表一个文件目录
     * @param map：存储文件名的Map集合
     */
    public void listFile(File file, List<String> map) {
// 如果file代表的不是一个文件，而是一个目录
        if (!file.isFile()) {
// 列出该目录下的所有文件和目录
            File files[] = file.listFiles();
// 遍历files[]数组
            for (File f : files) {
// 递归
                listFile(f, map);
            }
        } else {
            map.add(file.getName());
        }
    }

}
